﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;
using UncleBobApplicantWebService.ApplicantWebService;



namespace UncleBobApplicantWebService.Controllers
{
    public class HMMainMenuController : Controller
    {
        private UncleBobApplicantWebService.ApplicantWebService.WebServiceClient mApplicantClient =
            new UncleBobApplicantWebService.ApplicantWebService.WebServiceClient();
       


        public ActionResult MainMenu()
        {
            HMRegisterModel hmRegMod = Session[SessionKeys.USER] as HMRegisterModel;
            return View(hmRegMod);
        }
        

        public ActionResult ViewApplication(string collection)
        {
            string appId = collection;
            if (appId == null)
            {
                appId = (Session[SessionKeys.JOBAPPLICATION] as JobApplication).AppID.ToString();
            }

            JobApplication jobApp = new JobApplication();
            jobApp.AppID = Convert.ToInt32(appId);
            Message msg = new Message();
         
            HMRegisterModel hmModel = new HMRegisterModel();
            
            Message msgObj = new Message();
            msgObj.Data = jobApp;
            msgObj.Category = "get_hr_special_job_application";        

            //msg contains questions list
            msg = mApplicantClient.RequestService(msgObj);



            
            //Get user ID
            Message message = new Message(MessageTypes.SERVICE_GET_MANAGER_JOB_APPLICATIONS);
            message = mApplicantClient.RequestService(message);
            HMRegisterModel hmReg = (HMRegisterModel)message.Data;

            RegisterModel regMod = new RegisterModel();

            foreach (var applications in hmReg.jobApps)
            {
                if (applications.AppID == jobApp.AppID)
                {
                    regMod.UserID = applications.UserID;
                    break;
                }
            }

            Message appMsg = new Message();
            appMsg.Data = regMod;
            appMsg.Category = "get_user_information";
            appMsg = mApplicantClient.RequestService(appMsg);


            HMRegisterModel questions = (HMRegisterModel)msg.Data;
            questions.RegModel = (RegisterModel)appMsg.Data;

            //change app status to viewed
            Message statusMsg = new Message();
            statusMsg.Data = jobApp;
            statusMsg.Category = "edit_app_status";
            statusMsg = mApplicantClient.RequestService(statusMsg);

            jobApp = statusMsg.Data as JobApplication;
            jobApp.UserID = regMod.UserID;

            msg = new Message(MessageTypes.SERVICE_GET_USER_JOB_APPLICATION);
            msg.Data = jobApp;
            msg = mApplicantClient.RequestService(msg);

            Session.Add(SessionKeys.JOBAPPLICATION, msg.Data as JobApplication);
            questions.jobApps.Add(msg.Data as JobApplication);

            return View(questions);
        }

        
        public ActionResult FormAction(FormCollection collection, String check, HMRegisterModel model)
        {
            JobApplication jobApp = new JobApplication();
            Message msg;

            // We need to set up the job application model.
            string appID = collection["application"] as string;
            if (appID != null)
            {
                jobApp.AppID = int.Parse(appID);

                msg = new Message(MessageTypes.SERVICE_GET_USER_JOB_APPLICATION);
                msg.Data = jobApp;
                msg = mApplicantClient.RequestService(msg);

                jobApp = msg.Data as JobApplication;
                Session.Add(SessionKeys.JOBAPPLICATION, jobApp);
            }

            if (collection["previous"] != null)
                return RedirectToAction("MainMenu", "HMMainMenu");            

            if (collection["goBack"] != null)
                return RedirectToAction("ApplicantList", "HMMainMenu");

            if (model.SelectedDetail != null)
            {
                string app = model.SelectedDetail.FirstName;
            }

            if (collection["viewApp"] != null)
            {
                string application = collection["application"] as string;
                return RedirectToAction("ViewApplication", new { collection = application });
            }

            if (collection["reject"] != null)
            {
                jobApp = Session[SessionKeys.JOBAPPLICATION] as JobApplication;               
                if (!(jobApp == null))
                {
                    jobApp.Approved = false;
                    jobApp.Rejected = true;

                    msg = new Message(MessageTypes.SERVICE_EDIT_JOB_APPLICATION_STATUS);
                    msg.Data = jobApp;
                    mApplicantClient.RequestService(msg);

                    Session.Add(SessionKeys.JOBAPPLICATION, jobApp);
                }
               
                return RedirectToAction("ApplicantList", "HMMainMenu");
            }

            if (collection["approve"] != null)
            {

                jobApp = Session[SessionKeys.JOBAPPLICATION] as JobApplication;
                if (!(jobApp == null))
                {
                    jobApp.Rejected = false;
                    jobApp.Approved = true;

                    msg = new Message(MessageTypes.SERVICE_EDIT_JOB_APPLICATION_STATUS);
                    msg.Data = jobApp;
                    mApplicantClient.RequestService(msg);

                    Session.Add(SessionKeys.JOBAPPLICATION, jobApp);
                }
                
                return RedirectToAction("ApplicantList", "HMMainMenu");
            }

            if (collection["hire"] != null)
            {
                jobApp = Session[SessionKeys.JOBAPPLICATION] as JobApplication;
                if (!(jobApp == null))
                {
                    jobApp.Rejected = false;
                    jobApp.Hired = true;

                    msg = new Message(MessageTypes.SERVICE_EDIT_JOB_APPLICATION_STATUS);
                    msg.Data = jobApp;
                    mApplicantClient.RequestService(msg);

                    Session.Add(SessionKeys.JOBAPPLICATION, jobApp);

                    // Now we need to remove one of the available positions.
                    msg = new Message(MessageTypes.SERVICE_EDIT_EXISTING_JOB);
                    jobApp.Job.PositionsAvailable--;
                    msg.Data = jobApp.Job;
                    msg = mApplicantClient.RequestService(msg);                    
                }
               

                return RedirectToAction("ApplicantList", "HMMainMenu");
            }

          
            if (collection["cmdViewApplicants"] != null)
            {
                var test = check;
                return RedirectToAction("ApplicantList", "HMMainMenu");
            }

            if (collection["cmdRequestOpening"] != null)
            {
                return RedirectToAction("MainMenu", "HMMainMenu");
            }

            if (collection["cmdEditExisting"] != null)
            {
                CreateListOfJobs();
                return RedirectToAction("JobsListView", "StaffingExpertMenu");
            }

            if (collection["addcomment"] != null)
            {                
                return RedirectToAction("HMComments", "HMMainMenu");
            }

            if (collection["commentAdded"] != null)
            {
                //Do something with string???
                string application = collection["textComment"] as string;
                return RedirectToAction("CommentAdded", "HMMainMenu");
            }

            return RedirectToAction("Index", "Welcome");
        }

        /*
       * CreateListOfJobs : bool
       * -------------------------------------------------------------------------
       * If a List<JobModel> can be retrieved from the database, store the List in 
       * the Session and return "true". Otherwise, return "false".
       */
        private bool CreateListOfJobs()
        {
            // Create a new list of jobs and a corresponding message to query the DB
            Message msg = new Message(MessageTypes.SERVICE_GET_ALL_JOBS);

            // Query the DB for the list of jobs and add it to the session.
            msg = mApplicantClient.RequestService(msg);

            // If we couldn't retrieve the JobList from the DB, we ran into an error
            if (null == (msg.Data as List<JobModel>))
            {
                return false;
            }
            else
            {
                Session.Add(SessionKeys.JOBSLIST, msg.Data as List<JobModel>);
                return true;
            }
        }

        public ActionResult ApplicationPage(FormCollection collection)
        {
            if (collection["viewApp"] != null)
            {
                return RedirectToAction("ViewApplication", "HMMainMenu");
            }

           
            Message msg;

            HMRegisterModel hmModel = new HMRegisterModel();
            msg = new Message(MessageTypes.SERVICE_GET_JOB_APPLICANTS);

            msg = mApplicantClient.RequestService(msg);


            return View(hmModel);
        }



        public ActionResult ApplicantList(HMRegisterModel model)
        {
            Message msg;

            if (model.SelectedDetail != null)
            {
                string app = model.SelectedDetail.FirstName;
            }

                     

            HMRegisterModel hmModel = new HMRegisterModel();
            msg = new Message(MessageTypes.SERVICE_GET_JOB_APPLICANTS);
            msg = mApplicantClient.RequestService(msg);
            
            JobApplication jobApp = new JobApplication();
            Message message = new Message(MessageTypes.SERVICE_GET_MANAGER_JOB_APPLICATIONS);
            message = mApplicantClient.RequestService(message);
            HMRegisterModel applications = (HMRegisterModel)message.Data;



            HMRegisterModel regMod = new HMRegisterModel();
            regMod.apps = new List<HMRegisterModel>();
            
            foreach (JobApplication jobApplication in applications.jobApps)
            {
                HMRegisterModel tempApp = new HMRegisterModel();
                
                //Get user name and info from user id
                RegisterModel tempReg = new RegisterModel();
                tempReg.UserID = jobApplication.UserID;
                Message messageReg = new Message();
                messageReg.Category = "get_user_information";
                messageReg.Data = tempReg;
                message = mApplicantClient.RequestService(messageReg);
                RegisterModel hmReg = (RegisterModel)message.Data;

                //Assign values to tempApp                
                tempApp.FirstName = hmReg.FirstName;
                tempApp.MI = hmReg.MI;
                tempApp.LastName = hmReg.LastName;
                tempApp.ViewedStatus = jobApplication.Status;
                               
                //Add object to list
                tempApp.jobApps = new List<JobApplication>();
                tempApp.jobApps.Add(jobApplication);

                regMod.apps.Add(tempApp);
            }


                       
            return View(regMod);
            


            //HMRegisterModel applicants = (HMRegisterModel)msg.Data;
            //return View(hmModel.apps);
        }


        public ActionResult CommentAdded(FormCollection collection)
        {            
            JobApplication jobApp = Session[SessionKeys.JOBAPPLICATION] as JobApplication;
            Message msg;

            if (jobApp.CommentContainer.Comment == "")
            {
                CommentModel commentHolder = new CommentModel();
                commentHolder.AppID = jobApp.AppID;
                commentHolder.UserID = jobApp.UserID;
                commentHolder.Comment = " [ " + collection.Get("comment") + " - " + DateTime.UtcNow + " ]";
                jobApp.CommentContainer = commentHolder;
                msg = new Message(MessageTypes.SERVICE_ADD_COMMENT);
                msg.Data = commentHolder;
            }
            else
            {
                msg = new Message(MessageTypes.SERVICE_EDIT_COMMENT);
                jobApp.CommentContainer.Comment += " [ " + collection.Get("comment") + " - " + DateTime.UtcNow + " ]";
                msg.Data = jobApp.CommentContainer;
            }
                        
            msg = mApplicantClient.RequestService(msg);

            return RedirectToAction("ViewApplication", "HMMainMenu");
        }


        public ActionResult HMComments()
        {
            return View();
        }
    }
}






